#ifndef CONSOLE_WRITER_BOX_H_JM_20120626
#define CONSOLE_WRITER_BOX_H_JM_20120626

// ##
#include "../RootBox.h"
#include "ConsoleWriterBoxDescriptor.h"
// ## C++
#include <iostream>

namespace jm4bbx {

	template<typename policy/*, class BoxDescriptor*/>
	class ConsoleWriterBox : public Box/*<BoxDescriptor>*/ { 
	public:
		typedef ConsoleWriterBoxDescriptor BoxIO;

		ConsoleWriterBox(bobox::box_id_type id, bobox::box_tid_type tid, 
			bobox::request *req, const bobox::parameters &params) 
			: Box(id, tid, req, params) {

				id_ = CONSOLE_WRITER_COUNTER++;

				psss_ = params.has_parameter("Psss");
					
			
		}

		virtual void initImpl() override {

			add_group(BoxIO::INPUT_GROUP, BoxIO::INPUT_COLUMN, 
				get_input_descriptors(BoxIO::IN_ARC));

			wait4Receive();
			wait4SignalsIn();
		}

		virtual void sync_mach_etwas() override {
			Group g = get_group(BoxIO::INPUT_GROUP);


			if (!psss_) {
				//for (; g.rdpos_ < g.rdend_; ++g.rdpos_) {
				for (; g.getReadPosition() < g.getReadEnd(); g.moveReadPosition()) {
					std::stringstream stream;
					stream << "writer(" << id_ << "): |	";
					for (columnType columnOffset = BoxIO::INPUT_COLUMN; 
						columnOffset < g.getColumnCount()/*columns_.size()*/; ++columnOffset) {				

						policy::value_type value = 
							get_data<policy>(columnOffset)[g.getReadPosition()/*rdpos_*/];
						stream  << value << "	|	";
					}
					std::cout << stream.str() << std::endl;
				}
			}
			if (g.isPoisoned()/*poisoned_*/) {
				static int cc = 0;
				++cc;
				if (cc == 2) {
					sendSignalsOut();
				}
			}
			//sendSignalsOut();
			wait4Receive();
		}
	protected:
		static int CONSOLE_WRITER_COUNTER;
		bool psss_;
	private:
		int id_;

		void wait4Receive() {
			receive(BoxIO::IN_ARC, BoxIO::INPUT_GROUP);;			
		}

	};

	template<typename policy/*, class BoxDescriptor*/>
	int jm4bbx::ConsoleWriterBox<policy/*, BoxDescriptor*/>::CONSOLE_WRITER_COUNTER = 0;

	class StringConsoleWriterBox 
		: public ConsoleWriterBox<bobox::string_policy/*, StringConsoleWriterBoxDescriptor*/> {
	public:
		//static const char* BOX_ID; 

		//typedef GenericBoxModel<StringConsoleWriterBox> BoxModel;

		StringConsoleWriterBox(bobox::box_id_type id, bobox::box_tid_type tid, 
			bobox::request *req, const bobox::parameters &params) 
			: ConsoleWriterBox(id, tid, req, params) { }
	};
	class IntegerConsoleWriterBox 
		: public ConsoleWriterBox<bobox::int_policy/*, IntegerConsoleWriterBoxDescriptor*/> {
	public: 
		//static const char* BOX_ID; 

		//typedef GenericBoxModel<IntegerConsoleWriterBox> BoxModel;

		IntegerConsoleWriterBox(bobox::box_id_type id, bobox::box_tid_type tid, 
			bobox::request *req, const bobox::parameters &params) 
			: ConsoleWriterBox(id, tid, req, params) { }
	};

} // namespace jm4bbx

#endif
